<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				initValue:{
					formUpload:[]
				},
				userCacheKey:"atc-a-projectAdvice-user-cache",
				receiveInstCacheKey:"atc-a-projectAdvice-receiveInst-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId',"orgId"])
		},
		methods:{
			getPost(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPostList",
					{
						"user.id":id
					}).then(res=>{
						if(res.length){
							_that.$refs.autoform.changeEditor('title1','postId','data',configSelectData(res,'id','posname'))
							if(_that.edit){
								_that.$refs.autoform.initSelectIndex('title1','postId')
							}
						}
					})	
			},	
			onSelectZpos(item){
				let _that = this
				_that.$refs.autoform.formdata.zinstId = item.instId
			}, 
			formSubmit(){
				let _that = this 
				delete _that.formdata.fileList
				_that.formdata.fileListStr = JSON.stringify(_that.formdata.formUpload)
				_that.formdata.formUpload = JSON.stringify(_that.formdata.formUpload)
				_that.formdata.orgId = _that.orgId
				_that.formdata.newFileFlag = true
				if(_that.edit){
					_that.formdata.newFileFlag = false
					for(let index in _that.formdata.formUpload){
						if(!_that.formdata.formUpload[index].hasOwnProperty('id')){
							_that.formdata.newFileFlag = true
						}
					}
					_that.om.formSave('projectAdvise/doUpdateByH5',_that.formdata,_that)
				}else{
					_that.om.formSave('projectAdvise/doAddByH5',_that.formdata,_that)
				}
			},
			onSelectPost(item){
				let _that = this
				_that.$refs.autoform.formdata.instId = item.instId
				_that.$refs.autoform.formdata.instName = item.instName
			},
			
			intLoad(options){
				let _that = this
				let ajaxArr = []
				ajaxArr.push(urlUtil.zposUrl(options.code))
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"projectAdvise/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData)
					changeEditor('title1', 'userId', {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.userCacheKey,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					changeEditor('title1','receiveInstId',{
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
								containBoard:true
							}),
							method:"post",
							namefield:'instName',
							searchfield:"instName",
							initload:true,
							url:'tinstitution/findNotBoard1',
							cachename:_that.receiveInstCacheKey,
							placeholder:"请输入部门名称"
						}
					},null,mainData)
					changeEditor('title9','formUpload',"fileUploadKey","20",mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[1].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"formUpload",res[1].value.fileList)
						_that.formdata.formUpload = res[1].value.fileList
						
						changeEditor('title9','formUpload',"fileUploadKey","20",mainData)
						if(_that.formdata.formUpload){
							var tempFileList = []
							var fileUrlList = _that.formdata.formUpload.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("title9","formUpload",'fileUrlList',fileUrlList,mainData)
							changeEditor("title9","formUpload","tempFileList",tempFileList,mainData)
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex('topinfo','zposId')
							_that.getPost(_that.$refs.autoform.formdata.userId)
						})
						
					}
					
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:function(res){
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.userName = d.username
							formdata.userId = d.id
							_that.getPost(d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.receiveInstCacheKey,
					success:function(res){
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.receiveInstId = d.id
							formdata.receiveInstName = d.instName
							_that.om.request(
								'tposition/getPrimary',
								{
									instId:d.id
								}
							).then(res =>{
								if(res.value){
							 	    _that.$refs.autoform.formdata.receivePostName = res.value.posname
									_that.$refs.autoform.formdata.receivePostId = res.value.id
									_that.$refs.autoform.formdata.receiveUserId = res.value.userId
							 		_that.$refs.autoform.formdata.receiveUserName = res.value.userName
								}
							})
							
							uni.removeStorage({
								key:_that.receiveInstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
